package org.linlinjava.litemall.db.dao;

import org.apache.ibatis.annotations.*;
import org.linlinjava.litemall.db.domain.DevicesOrder;
import org.linlinjava.litemall.db.enums.OrderStatus;

@Mapper
public interface DevicesOrderMapper {
    @Insert("INSERT INTO devices_orders(order_no, user_id, device_id, quantity, total_price, status) " +
            "VALUES(#{orderNo}, #{userId}, #{deviceId}, #{quantity}, #{totalPrice}, #{status.code})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(DevicesOrder order);

    @Select("SELECT * FROM devices_orders WHERE id = #{id}")
    @Results({
            @Result(property = "status", column = "status",
                    javaType = OrderStatus.class,
                    typeHandler = org.apache.ibatis.type.EnumOrdinalTypeHandler.class)
    })
    DevicesOrder selectById(Integer id);

    @Update("UPDATE devices_orders SET status = #{status.code}, update_time = NOW() WHERE id = #{id}")
    int updateStatus(@Param("id") Integer id, @Param("status") OrderStatus status);
}